#pragma once
#define _CRT_SECURE_NO_WARNINGS

#include<assert.h>
#include<iostream>
#include<string.h>
using namespace std;
namespace zjw
{
	class string
	{public:
		typedef char* iterator;
		typedef   const char* const_iterator;
		iterator begin()
		{
			return _str;
		
		
		
		}
		iterator end()
		{
			return _str+_size;



		}
		const_iterator begin() const
		{
			return _str;



		}
		const_iterator end() const
		{
			return _str + _size;



		}
		string(const char* str ="")
			:_size(strlen(str))
		{
			_capacity = _size;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}
		~string()
		{ 
			delete[]_str;
			_str = nullptr;
			_size = _capacity = 0;

		
		
		
		
		}
		size_t size()
		{
			return _size;
		
		
		
		
		}
		size_t capacity()
		{
			return _capacity;

		
		}
		char& operator[](size_t pos)
		{
			assert(pos<_size);
			return _str[pos];

		
		
		}
		const char& operator[](size_t pos) const
		{
			assert(pos < _size);
			return _str[pos];



		}
		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n + 1];
				strcpy(tmp, _str);
				delete[]_str;
				_str = tmp;
				_capacity = n;

			}
		
		
		
		
		
		}
		void push_back(char ch)
		{
			if (_capacity == _size)
			{
				reserve(_capacity == 0 ? 4 : _capacity * 2);

			}
			_str[_size] = ch;
			_size++;
			_str[_size] = '\0';


		
		
		
		
		}
		void append(const char* str)
		{
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);

			}
			strcpy(_str + _size, str);
			_size += len;
			//_str[_size] = '\0';

		}
		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;

		
		
		}
		string& operator+=(const char* str)
		{
			append(str);
			return *this;

		



		}
		void insert(size_t pos, char ch)
		{
			assert(pos<=_size);
			if (_capacity == _size)
			{
				reserve(_capacity == 0 ? 4 : _capacity * 2);

			}
			size_t end = _size+1;
			while (end >pos)
			{

				_str[end] = _str[end-1];
				end--;


			}
			_str[pos] = ch;
			_size++;

		
		
		
		
		
		}
		void earse(size_t pos, size_t len = npos)
		{
			assert(pos < _size);
			if (len == npos || pos > _size-len)
			{
				_str[pos] = '\0';
				_size = pos;
				

			}
			else
			{
				strcpy(_str + pos, _str + pos + len);
				_size -= len;

			}
			
		
		
		
		
		
		
		}
		bool empty()
		{
		
			return _size==0;
		
		}
	 public:
		 static const int  npos;




	private:
		char* _str;
		int _size;
		int _capacity;



	};
	const int string::npos = -1;
	 void test1()
	{
		 string str;
		 str += 'a';
		
		 str+='b';
		 str += "beijing";

		
		 string::iterator it = str.begin();
		 while (it != str.end())
		 {
			 cout << *it << " ";
			 it++;

		 }

	
	
	
	
	}
	 void test2()
	 {
		 string str;
		 str.push_back('a');
		 str.push_back('a');
		 str.push_back('b');
		 str.push_back('a');
		 str.push_back('a');
		 string::iterator it = str.begin();
		 while (it != str.end())
		 {
			 cout << *it << " ";
			 it++;

		 }
		 cout << str[2];





	 }
	 void test3()
	 {
		 string str;
		 str.append("stringbj");
		 for (auto& e : str)
		 {
			 cout << e << " ";
		 }
	 
	 
	 }
	 void print_string(const string it)
	 {
		 string::const_iterator res = it.begin();
		 while (res != it.end())
		 {
			 cout << *res << " ";
			 ++res;



		 }




	 }
	  void test4()
	 {
		 string str;
		 str += "abc";
		 str.append("stringbj");
		 string::iterator it = str.begin();
		 while (it != str.end())
		 {
			 cout << *it << " ";
			 it++;

		 }
		 str.insert(0, 'g');
		 for (auto& e : str)
		 {
			 cout << e << " ";
		 }
		 cout << endl;
		// str.earse(1,string::npos);*/
		/* for (auto e : str)
		 {
			 cout << e << " ";
		 }*/
		// print_string(str);



	 }
	





}
